import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'app_color.dart';

class AppTab extends StatelessWidget {
  const AppTab(
    this.tabs,
    this.tabController, {
    this.labelColor,
    this.labelStyle,
    this.unselectedLabelColor,
    this.unselectedLabelStyle,
    this.indicator,
    this.indicatorPadding,
    this.indicatorSize,
    this.isScrollable,
    this.labelPadding,
    this.tabAlignment,
    this.height,
    this.children,
    this.onTap,
    super.key,
  });

  final List<String> tabs;
  final TabController tabController;
  final Color? labelColor;
  final TextStyle? labelStyle;
  final Color? unselectedLabelColor;
  final TextStyle? unselectedLabelStyle;
  final Decoration? indicator;
  final EdgeInsetsGeometry? indicatorPadding;
  final TabBarIndicatorSize? indicatorSize;
  final bool? isScrollable;
  final EdgeInsetsGeometry? labelPadding;
  final TabAlignment? tabAlignment;
  final double? height;
  final List<Widget>? children;

  final ValueChanged<int>? onTap;

  @override
  Widget build(BuildContext context) {
    final tab = TabBar(
      controller: tabController,
      isScrollable: isScrollable ?? false,
      dividerHeight: 0,
      labelPadding: labelPadding ?? EdgeInsets.zero,
      tabAlignment: tabAlignment ?? TabAlignment.fill,
      labelColor: labelColor ?? AppColor.white,
      labelStyle:
          labelStyle ?? TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold),
      unselectedLabelColor: unselectedLabelColor ?? AppColor.white_60,
      unselectedLabelStyle: unselectedLabelStyle ?? TextStyle(fontSize: 16.sp),
      indicator:
          indicator ??
          UnderlineTabIndicator(
            borderRadius: BorderRadius.circular(6).r,
            borderSide: BorderSide(color: AppColor.white, width: 2.w),
          ),
      indicatorPadding:
          indicatorPadding ?? const EdgeInsets.symmetric(horizontal: 6).w,
      indicatorSize: indicatorSize ?? TabBarIndicatorSize.label,
      splashFactory: NoSplash.splashFactory,
      overlayColor: WidgetStateProperty.resolveWith<Color?>((
        Set<WidgetState> states,
      ) {
        return states.contains(WidgetState.focused) ? null : Colors.transparent;
      }),
      tabs: children ?? tabs.map((e) => Text(e)).toList(),
      onTap: onTap,
    );

    return tab;
  }

  factory AppTab.rank(List<String> tabs, TabController tabController) {
    return AppTab(
      tabs,
      tabController,
      isScrollable: true,
      labelStyle: TextStyle(fontSize: 13.sp),
      unselectedLabelStyle: TextStyle(fontSize: 13.sp),
      indicator: BoxDecoration(),
      indicatorSize: TabBarIndicatorSize.tab,
      tabAlignment: TabAlignment.start,
      children: tabs.map((e) {
        final isFirst = tabs.indexOf(e) == 0;
        return Container(
          margin: EdgeInsets.only(left: isFirst ? 18 : 6).w,
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8).w,
          decoration: BoxDecoration(
            color: Color(0xFF292B34),
            borderRadius: BorderRadius.circular(4).r,
          ),
          child: Center(child: Text(e)),
        );
      }).toList(),
    );
  }

  factory AppTab.category(List<String> tabs, TabController tabController) {
    return AppTab(
      tabs,
      tabController,
      isScrollable: true,
      labelStyle: TextStyle(fontSize: 13.sp),
      labelColor: Color(0xFFE73A57),
      unselectedLabelStyle: TextStyle(fontSize: 13.sp),
      indicator: BoxDecoration(),
      indicatorSize: TabBarIndicatorSize.tab,
      tabAlignment: TabAlignment.start,
      children: tabs.map((e) {
        final isFirst = tabs.indexOf(e) == 0;
        return Container(
          margin: EdgeInsets.only(left: isFirst ? 18 : 6).w,
          padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 5).w,
          decoration: BoxDecoration(
            color: Color(0xFF292B34),
            borderRadius: BorderRadius.circular(20).r,
          ),
          child: Center(child: Text(e)),
        );
      }).toList(),
    );
  }

  factory AppTab.bingWatch(List<String> tabs, TabController tabController) {
    return AppTab(
      tabs,
      tabController,
      isScrollable: false,
      labelStyle: TextStyle(fontSize: 17.sp),
      unselectedLabelStyle: TextStyle(fontSize: 17.sp),
      indicatorSize: TabBarIndicatorSize.label,
      tabAlignment: TabAlignment.center,
      labelPadding: const EdgeInsets.all(10).w,
      indicatorPadding: const EdgeInsets.symmetric(horizontal: 12).w,
    );
  }

  factory AppTab.forYou(List<String> tabs, TabController tabController) {
    return AppTab.bingWatch(tabs, tabController);
  }

  factory AppTab.videoInfo(
    List<String> tabs,
    TabController tabController,
    ValueChanged<int>? onTap,
  ) {
    return AppTab(
      tabs,
      tabController,
      isScrollable: true,
      labelStyle: TextStyle(fontSize: 15.sp),
      unselectedLabelStyle: TextStyle(fontSize: 15.sp),
      unselectedLabelColor: AppColor.white_40,
      indicatorSize: TabBarIndicatorSize.label,
      tabAlignment: TabAlignment.start,
      labelPadding: const EdgeInsets.all(10).w,
      indicatorPadding: const EdgeInsets.symmetric(horizontal: 10).w,
      onTap: onTap,
    );
  }

  factory AppTab.videoSelectEpisode(
    List<String> tabs,
    TabController tabController,
  ) {
    return AppTab(
      tabs,
      tabController,
      isScrollable: true,
      labelStyle: TextStyle(fontSize: 14.sp),
      unselectedLabelStyle: TextStyle(fontSize: 14.sp),
      unselectedLabelColor: AppColor.white_40,
      tabAlignment: TabAlignment.start,
      labelPadding: const EdgeInsets.all(10).w,
      indicator: BoxDecoration(),
      indicatorPadding: const EdgeInsets.symmetric(horizontal: 10).w,
    );
  }
}
